package com.wyl.auth.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wyl.auth.dto.SysDictTypeDto;
import com.wyl.auth.entity.SysDictData;
import com.wyl.auth.entity.SysDictType;
import com.wyl.auth.exception.BusinessException;
import com.wyl.auth.mapper.SysDictTypeMapper;
import com.wyl.auth.req.SysDictTypeReq;
import com.wyl.auth.service.SysDictDataService;
import com.wyl.auth.service.SysDictTypeService;
import com.wyl.auth.vo.SysDictTypeVo;
import com.wyl.common.CommonPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @Description 字典类型表
* @Author WuYiLong
* @Date 2025-04-27
*/
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService {

    @Autowired
    private SysDictDataService sysDictDataService;

    @Override
    public void save(SysDictTypeDto dto) {
        if(dto.getId() != null) {
            SysDictType sysDictType = this.getById(dto.getId());
            BeanUtils.copyProperties(dto, sysDictType);
            sysDictType.updateById();
        }else{
            SysDictType sysDictType = new SysDictType();
            BeanUtils.copyProperties(dto, sysDictType);
            sysDictType.insert();
        }
    }

    @Override
    public CommonPage<SysDictTypeVo> page(SysDictTypeReq req) {
        Page<SysDictType> page = new Page<>(req.getCurrentPage(), req.getPageSize());
        LambdaQueryWrapper<SysDictType> wrapper = Wrappers.lambdaQuery(SysDictType.class);
        wrapper.like(StrUtil.isNotBlank(req.getTypeName()), SysDictType::getTypeName, req.getTypeName());
        wrapper.eq(StrUtil.isNotBlank(req.getTypeCode()), SysDictType::getTypeCode, req.getTypeCode());
        page = page(page, wrapper);
        List<SysDictTypeVo> sysDictTypeVos = page.getRecords().stream().map(m -> {
            SysDictTypeVo sysDictTypeVo = new SysDictTypeVo();
            BeanUtils.copyProperties(m, sysDictTypeVo);
            return sysDictTypeVo;
        }).collect(Collectors.toList());

        CommonPage<SysDictTypeVo> commonPage = new CommonPage<>(req.getCurrentPage(), req.getPageSize());
        commonPage.setList(sysDictTypeVos);
        commonPage.setTotal((int)page.getTotal());
        return commonPage;
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        if(ids == null || ids.isEmpty()) {
            return;
        }
        ids.forEach(id -> {
            LambdaQueryWrapper<SysDictData> sysDictDataLambdaQueryWrapper = Wrappers.lambdaQuery(SysDictData.class);
            sysDictDataLambdaQueryWrapper.eq(SysDictData::getTypeId, id);
            long count = sysDictDataService.count(sysDictDataLambdaQueryWrapper);
            if(count > 0) {
                throw new BusinessException("尚有字典数据，暂不能删除");
            }
        });

        removeByIds(ids);
    }
}
